//
//  EnergyConsumptionAnalysis.m
//  cocoapodText
//
//  Created by apple on 16/9/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import <Foundation/Foundation.h>
#import "EnergyConsumptionAnalysis.h"
@interface EnergyConsumptionAnalysis ()<ApiEnergyInfoManagerDelegate,GYEnergyAnalysisViewDelegate>
@end

@implementation EnergyConsumptionAnalysis

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    [self GetMainContentView];
}

/**
 * 边界
 * top:(0, 0, kScreenW, 10)
 * bottom:(0, kScreenH-10, kScreenW,10)
 * left:(0, 0, 10, kScreenH)
 * right:(kScreenW-170, 0, 10, kScreenH)
 */
-(void)GetMainContentView{
    //内容栏
    _energyAnalysisView=[[EnergyAnalysisView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:TabChildContentW+10], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [_energyAnalysisView setDelegate:self];
    [self.view addSubview:_energyAnalysisView];
}

-(void)initEnergyData{
    
    ApiEnergyInfoManager *manager=[[ApiEnergyInfoManager alloc]init];
    [manager setDelegate:self];
    //初始化
    [manager fsgy_api_energy_info_withRequestCode:ECAInit];
    //默认分项菜单选中
    [_energyAnalysisView switchChangeTrend:@"4"];
    [_energyAnalysisView changeTabItemStyle:0];
    [manager fsgy_api_energy_info_withRequestCode:ECATabItem esCode:@"01" orgNo:@"520100000" structType:@"4" areaId:@"520100000:4"];
}
-(void)changeTabItemStyle:(NSInteger)tag{
    //0,1,2(区域，分项，行业)
    [self.energyAnalysisView changeTabItemStyle:tag];
}

-(void)onInitFristChartTableData:(EnergyAnalysisView *)energyAnalysisView esCode:(NSString *)esCode{
    ApiEnergyInfoManager *manager=[[ApiEnergyInfoManager alloc]init];
    [manager setDelegate:self];
    //默认曲线(贵阳市：电：日：一个月内数据)
    [manager fsgy_api_energy_info_withRequestCode:ECAChart esCode:esCode orgid:@"520100000:4:贵阳市:00" structType:@"4" xslx:@"1" startTime:[CommonTool getDateTimeDifferenceWithYear:0 Month:-1 Day:0 Rule:@"yyyyMMdd"] endTime:[CommonTool getDateTimeDifferenceWithYear:0 Month:0 Day:-1 Rule:@"yyyyMMdd"]];
}

-(void)onEnergyInfo:(ApiEnergyInfoManager *)manager result:(NSDictionary *)result code:(NSString *)code{
    if ([code isEqualToString:ECAInit]) {
        //tab选项卡菜单
        //NSArray *tabOrderList=[result objectForKey:@"tabOrderList"];
        //NSLog(@"tabOrderList=%@",tabOrderList);
        //能源类型
        NSArray *typeMapList=[result objectForKey:@"typeMapList"];
        [_energyAnalysisView setGYMenuEnergyTypeData:typeMapList];
    }else if ([code isEqualToString:ECATabItem]) {
       //分项行业目录
        NSArray *lstTree=[result objectForKey:@"lstTree"];
        [_energyAnalysisView setGYMenuTableData:lstTree];
    }else if([code isEqualToString:ECAChart]){
        NSString *esunit=[CommonTool getStringFromNSDictionary:result key:@"esunit" defaultValue:@""];
        NSArray *xAxis=[result objectForKey:@"xAxis"];
        NSArray *series=[result objectForKey:@"series"];
        NSArray *legend=[result objectForKey:@"legend"];
        [_energyAnalysisView setEnergyTableWithxAxis:xAxis yAxiss:series legend:legend];
        [self onSetLineChartData:_energyAnalysisView xAxis:xAxis yAxiss:series legend:legend unit:esunit];
    }
}

-(void)onSetLineChartData:(EnergyAnalysisView *)energyAnalysisView xAxis:(NSArray *)xAxis yAxiss:(NSArray *)yAxiss legend:(NSArray *)legend unit:(NSString *)unit{
    //x轴文字
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < [xAxis count]; i++){
        [xVals addObject:[xAxis objectAtIndex:i]];
    }
    //y轴多折线
    NSMutableArray *dataYVals = [[NSMutableArray alloc] init];
    for (int z = 0; z < [legend count]; z++){
        NSMutableArray *values = [[NSMutableArray alloc] init];
        NSMutableArray *yAxis=[[yAxiss objectAtIndex:z] objectForKey:@"data"];
        for (int i = 0; i < [xAxis count]; i++) {
            NSString *valStr=[CommonTool getStringFromNSArray:yAxis key:i];
            double val=[valStr doubleValue];
            [values addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
        }
        [dataYVals addObject:values];
    }
    NSMutableArray *lineColors = [[NSMutableArray alloc] init];
    [lineColors addObject:FUIColorFromRGB(0x1f73af)];
    [lineColors addObject:FUIColorFromRGB(0x59cc9a)];
    [lineColors addObject:FUIColorFromRGB(0xff3a00)];
    [lineColors addObject:FUIColorFromRGB(0x1fcc00)];
    [lineColors addObject:FUIColorFromRGB(0x593aaf)];
    [lineColors addObject:FUIColorFromRGB(0xff739a)];
    [lineColors addObject:FUIColorFromRGB(0xffccaf)];
    [lineColors addObject:FUIColorFromRGB(0x1f3a9a)];
    [lineColors addObject:FUIColorFromRGB(0x597300)];
    [lineColors addObject:FUIColorFromRGB(0x43d282)];
    [lineColors addObject:FUIColorFromRGB(0x45c8dc)];
    [lineColors addObject:FUIColorFromRGB(0xf77d6a)];
    [lineColors addObject:FUIColorFromRGB(0x9b6ed0)];
    [lineColors addObject:FUIColorFromRGB(0xf76e82)];
    [lineColors addObject:FUIColorFromRGB(0x457dd0)];
    [lineColors addObject:FUIColorFromRGB(0x43c86a)];
    [lineColors addObject:FUIColorFromRGB(0xf76e82)];
    [lineColors addObject:FUIColorFromRGB(0x9b7ddc)];
    [lineColors addObject:FUIColorFromRGB(0xf7c882)];
    [lineColors addObject:FUIColorFromRGB(0x1f73af)];
    [lineColors addObject:FUIColorFromRGB(0x9bc8af)];
    [lineColors addObject:FUIColorFromRGB(0xf773dc)];
    [lineColors addObject:FUIColorFromRGB(0x1f7d82)];
    [lineColors addObject:FUIColorFromRGB(0x9b7382)];
    [lineColors addObject:FUIColorFromRGB(0xf77daf)];
    [lineColors addObject:FUIColorFromRGB(0x1fc8dc)];
    
    energyAnalysisView.xValsEnergyLine = xVals;
    energyAnalysisView.yValsEnergyLine = dataYVals;
    energyAnalysisView.lineColors=lineColors;
    energyAnalysisView.legend=legend;
    energyAnalysisView.unit=unit;
    //设置变化趋势曲线图数据
    [energyAnalysisView multipleLineChartData];
}

-(void)onQueryChartData:(EnergyAnalysisView *)energyAnalysisView esCode:(NSString *)esCode xslx:(NSString *)xslx startTime:(NSString *)startTime endTine:(NSString *)endTime orgid:(NSString *)orgid structType:(NSString *)structType{
    ApiEnergyInfoManager *manager=[[ApiEnergyInfoManager alloc]init];
    [manager setDelegate:self];
    [manager fsgy_api_energy_info_withRequestCode:ECAChart esCode:esCode orgid:orgid structType:structType xslx:xslx startTime:startTime endTime:endTime];
}

-(void)switchChangeTrend:(EnergyAnalysisView*)energyAnalysisView flag:(int)end{
    ApiEnergyInfoManager *manager=[[ApiEnergyInfoManager alloc]init];
    [manager setDelegate:self];
    switch (end) {
        case 0:
        {
            [_energyAnalysisView switchChangeTrend:@"4"];
            //4：区域
            [manager fsgy_api_energy_info_withRequestCode:ECATabItem esCode:@"01" orgNo:@"520100000" structType:@"4" areaId:@"520100000:4"];
        }
            break;
        case 1:
        {
            [_energyAnalysisView switchChangeTrend:@"3"];
            //3：分项
            [manager fsgy_api_energy_info_withRequestCode:ECATabItem esCode:@"01" orgNo:@"520100000" structType:@"3" areaId:@"520100000:3"];
        }
            break;
        case 2:
        {
            [_energyAnalysisView switchChangeTrend:@"5"];
            //5：行业
            [manager fsgy_api_energy_info_withRequestCode:ECATabItem esCode:@"01" orgNo:@"520100000" structType:@"5" areaId:@"520100000:5"];
        }
            break;
        default:
            break;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self initEnergyData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_energyAnalysisView dismissMenu];
}
@end
